package com.example.springbootsfzy.controller.sunkaixuan;

import com.example.springbootsfzy.domain.CourseFavorites;
import com.example.springbootsfzy.service.sunkaixuan.CourseFavoriteService;
import com.example.springbootsfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("CourseFavorite")
@CrossOrigin
public class CourseFavoriteController {

    @Autowired
    private CourseFavoriteService courseFavoriteService;

    @GetMapping("/getall")
    public List<CourseFavorites> getAll(){
        return courseFavoriteService.findAll();
    }


    @GetMapping("/selectByMemberId/{memberid}")
    @ResponseBody
    public R selectByMemberId(@PathVariable("memberid") int memberid) {
        return R.ok(courseFavoriteService.selectByMemberId(memberid));
    }


    @GetMapping("/selectByPrimaryKey/{id}")
    @ResponseBody
    public R selectByPrimaryKey(@PathVariable int id){
        return R.ok(courseFavoriteService.selectByPrimaryKey(id));
    }

    @DeleteMapping("/{id}")
    public R deleteByPrimaryKey(int id){
        int i = courseFavoriteService.deleteByPrimaryKey(id);
        if (i > 0) {
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    @PutMapping
    @ResponseBody
    public R updateByPrimaryKeySelective(@RequestBody CourseFavorites courseFavorites){
        int i = courseFavoriteService.updateByPrimaryKeySelective(courseFavorites);
        if (i > 0) {
            return R.ok("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    @PostMapping
    @ResponseBody
    public R insertSelective(@RequestBody CourseFavorites courseFavorites){
        int i = courseFavoriteService.insertSelective(courseFavorites);
        if (i > 0) {
            return R.ok("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    @DeleteMapping("/deleteByCourseIdAndMemberId/{courseId}/{memberId}")
    public R deleteByCourseIdAndMemberId(@PathVariable("courseId") Integer courseId, @PathVariable("memberId") Integer memberId){
        int i = courseFavoriteService.deleteByCourseIdAndMemberId(courseId, memberId);
        if (i > 0) {
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

}
